﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using kit206_A2.model;
using kit206_A2.database_controller;
using System.Collections.ObjectModel;

namespace kit206_A2.controller
{
    
    public class staff_list_controller
    {
        static private List<staff> staff_list;
        static private ObservableCollection<staff> viewableStaff;

        public ObservableCollection<staff> VisibleStaff_list { get { return viewableStaff; } set { } }


        public staff_list_controller()
        {
            staff_list = adapter.LoadAll_staff();
            viewableStaff = new ObservableCollection<staff>(staff_list);
        }

        public static staff get_staff_detail(int staff_id)
        {
            //var temp_detailList = adapter.LoadStaff_detaill(staff_id);

            return adapter.LoadStaff_detaill(staff_id);

        }

        public static void loadStaff_teaching(staff list)
        {

            adapter.LoadStaff_teaching(list);

        }


        public ObservableCollection<staff> GetViewableList()
        {
            return viewableStaff;
        }

        
        public static void Filter(Category category_value)
        {
            var selected = from staff e in staff_list
                           where category_value == Category.All || e.category == category_value
                           select e;
            viewableStaff.Clear();
            
            selected.ToList().ForEach(viewableStaff.Add);
        }
        private static bool check_stringContain(string text1, string text2)
        {
            /*indexof find text2 exists in the position of text1, using param (stringcomparison.ordinalignorecase) to compare string if less than 0 is not found,
            * if large or equal to 0 is empty text2 or text is found;   OrdinalIgnoreCase: ignore upper case or lower case of text 2.
            */
            return text1.IndexOf(text2, StringComparison.OrdinalIgnoreCase) >= 0;
        }

        public static void inputFilter(string input_value)
        {

            var input1_value = input_value; 
         
            //if input box is set
            if (input_value != null)
            {
                
                var input_selected = from staff e in staff_list
                                     where (check_stringContain(e.givenName, input_value) || check_stringContain(e.familyName, input_value))
                                     select e;
                viewableStaff.Clear();
                input_selected.ToList().ForEach(viewableStaff.Add);
            }
            else 
            {
                //reset to all staff list.
                var selected = from staff e in staff_list select e;
                viewableStaff.Clear();
                selected.ToList().ForEach(viewableStaff.Add);
            }
           
        }

    }
}

